package dao;

import beans.Morte;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class MorteDAO {
    
    PreparedStatement pst;
    ResultSet rs;
    
    public MorteDAO() {
    }
    
    public Morte load(int ano, int mes, int num_brinco) {
        Morte mrt = new Morte();

        String query = "SELECT * FROM morte WHERE ano = ? AND mes = ? AND num_brinco = ?";

        try {
            pst = Conexao.getConexao().prepareStatement(query);
            pst.setInt(1, ano);
            pst.setInt(2, mes);
            pst.setInt(3, num_brinco);

            rs = pst.executeQuery();

            if (rs.next()) {
                mrt.setAno(rs.getInt("ano"));
                mrt.setMes(rs.getInt("mes"));
                mrt.setNum_brinco(rs.getInt("num_brinco"));
                mrt.setData(rs.getDate("data"));
                mrt.setMotivo(rs.getString("motivo"));
            }

        } catch (SQLException ex) {
            ex.printStackTrace();
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (pst != null) {
                    pst.close();
                }
            } catch (SQLException ex) {
                            ex.printStackTrace();
            }
        }

        return mrt;
    }
    
    public void insert(Morte mrt) {
        String query = "INSERT INTO morte (ano, mes, num_brinco, data, motivo)"
                + " VALUES(?, ?, ?, ?, ?)";

        try {
            pst = Conexao.getConexao().prepareStatement(query);

            pst.setInt(1, mrt.getAno());
            pst.setInt(2, mrt.getMes());
            pst.setInt(3, mrt.getNum_brinco());
            pst.setDate(4, mrt.getData());
            pst.setString(5, mrt.getMotivo());

            pst.executeUpdate();

        } catch (Exception ex) {
                                        ex.printStackTrace();
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (pst != null) {
                    pst.close();
                }
            } catch (SQLException ex) {
                            ex.printStackTrace();
            }
        }
    }
    
    private void update(Morte mrt) {
        String query = "UPDATE morte"
                + "SET ano = ?, mes = ?, data = ?, motivo = ? "
                + "WHERE num_brinco = ?";

        try {
            pst = Conexao.getConexao().prepareStatement(query);

            pst.setInt(1, mrt.getAno());
            pst.setInt(2, mrt.getMes());
            pst.setDate(3, mrt.getData());
            pst.setString(4, mrt.getMotivo());
            pst.setInt(5, mrt.getNum_brinco());

            pst.executeUpdate();

        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (pst != null) {
                    pst.close();
                }
            } catch (SQLException ex) {
                ex.getLocalizedMessage();
            }
        }


    }

    public void save(Morte mrt) {
        String query = "SELECT COUNT(*) FROM morte WHERE ano = ? AND mes = ? AND num_brinco = ?";

        try {
            pst = Conexao.getConexao().prepareStatement(query);
            pst.setInt(1, mrt.getAno());
            pst.setInt(2, mrt.getMes());
            pst.setInt(3, mrt.getNum_brinco());

            rs = pst.executeQuery();
            rs.next();

            if (rs.getInt("count") != 0) {
                update(mrt);
            } else
                insert(mrt);

        } catch (SQLException ex) {
            ex.printStackTrace();

        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (pst != null) {
                    pst.close();
                }
            } catch (SQLException ex) {
                ex.getLocalizedMessage();
            }
        }
    }

    public ArrayList<Morte> loadRebanho(int mes, int ano) {
        ArrayList<Morte> mortes = new ArrayList<Morte>();

        String query = "SELECT * FROM morte WHERE mes = ? AND ano = ?";

        try {
            pst = Conexao.getConexao().prepareStatement(query);
			pst.setInt(1, mes);
			pst.setInt(2, ano);
			
            rs = pst.executeQuery();

            while (rs.next()) {
                Morte mrt = new Morte();

                mrt.setAno(rs.getInt("ano"));
                mrt.setMes(rs.getInt("mes"));
                mrt.setNum_brinco(rs.getInt("num_brinco"));
                mrt.setData(rs.getDate("data"));
                mrt.setMotivo(rs.getString("motivo"));

                mortes.add(mrt);
            }
        } catch (SQLException ex) {
            ex.getLocalizedMessage();
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (pst != null) {
                    pst.close();
                }
            } catch (SQLException ex) {
                ex.getLocalizedMessage();
            }
        }

        return mortes;
    }
}
